# -*- coding: utf-8 -*-
# @Time : 2022-01-19 11:20
# @Author : Z
# @Function :
import os

import pytest

from Common.Excel.excel import get_reader, get_writer
from Common.Logger import logger
from Keywords.inter_keys import Inter


class Conf:

    def __init__(self):
        # 保存一个模块里面所有的用例
        self.cases = []
        # 初始化关键字对象
        self.obj = None
        # 设置分组信息
        self.feature = ''
        self.feature_idx = 0
        self.story = ''
        self.story_idx = 0
        # 记录运行的sheet
        self.s_idx = 1
        # 记录运行的用例的序号
        self.c_idx = 1
        self.writer = None

    def runInterCase(self, case_file='', res_file=''):
        reader = get_reader(case_file)
        self.writer = get_writer(case_file, res_file)
        sheetname = reader.get_sheets()
        self.obj = Inter()
        self.case_type = 'inter'
        self.s_idx = 1
        self.c_idx = 1
        for sheet in sheetname:
            reader.set_sheet(sheet)
            self.writer.set_sheet(sheet)
            self.feature = sheet
            self.feature_idx += 1
            self.story_idx = 0
            lines = reader.readline()
            case = []
            for i in range(1, len(lines)):
                line = lines[i]
                if len(line[0]) > 1:
                    if case:
                        self.cases.append(case)
                        # 执行
                        if i == 1:
                            self.__run_pytest(1)
                        else:
                            self.__run_pytest()
                        case = []
                    self.story = line[0]
                    self.story_idx += 1

                    self.writer.row = i

                elif len(line[1]) > 1:
                    if case:
                        self.cases.append(case)
                    case = []
                    case.append(line)

                else:
                    case.append(line)

            if case:
                self.cases.append(case)

            if self.cases:
                # 执行
                self.__run_pytest()

            self.s_idx += 1

        self.writer.save_close()
        os.rename('./ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx - 1, self.c_idx - 1),
                  './ddt/test_%s.py' % self.case_type)

    def __run_pytest(self, is_start=0):
        # logger.info('执行')
        logger.info(self.cases)
        if self.c_idx == 1:
            os.rename('./ddt/test_%s.py' % (self.case_type,),
                      './ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx, self.c_idx))
        else:
            try:
                os.rename('./ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx, self.c_idx - 1),
                          './ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx, self.c_idx))
            except:
                os.rename('./ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx - 1, self.c_idx - 1),
                          './ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx, self.c_idx))
        pytest.main(
            ['-s', './ddt/test_%s_%s_%s.py' % (self.case_type, self.s_idx, self.c_idx),
             '--alluredir', './result'])

        self.c_idx += 1
        self.cases.clear()


conf = Conf()
